package com.erp.service.impl;

import com.erp.bean.*;
import com.erp.dao.*;
import com.erp.service.ReportFormService;
import com.erp.util.factroy.ObjectFactory;
import com.erp.vo.ReportFormsViewObject;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReportFormServiceImpl implements ReportFormService {

    OrderDao orderDao = ObjectFactory.getDAOInstance("com.erp.dao.impl.OrderDaoImpl",OrderDao.class);
    EmpDao empDao = ObjectFactory.getDAOInstance("com.erp.dao.impl.EmpDaoImpl",EmpDao.class);
    OrderStateDao stateDao = ObjectFactory.getDAOInstance("com.erp.dao.impl.OrderStateDaoImpl",OrderStateDao.class);
    CommodityDao commodityDao = ObjectFactory.getDAOInstance("com.erp.dao.impl.CommodityDaoImpl", CommodityDao.class);
    SupplierdDao supplierdDao = ObjectFactory.getDAOInstance("com.erp.dao.impl.SupplierdDaoImpl", SupplierdDao.class);
    ReportFormDao formDao = ObjectFactory.getDAOInstance("com.erp.dao.impl.ReportFormDaoImpl", ReportFormDao.class);

    @Override
    public boolean add(ReportForms form) {
        boolean flag = false;
        try {
            //向财务报表数据表添加数据
            flag = formDao.add(form)>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    public ReportFormsViewObject findById(int fid) {
        ReportFormsViewObject formsVo = null;
        try {
            //根据报表编号查询报表对象
            ReportForms form = formDao.findById(fid);
            //根据报表对象的订单编号查询出对应的订单信息
            Order order = orderDao.findById(form.getOid());
            //实例化报表显示对象并设置值
            formsVo = new ReportFormsViewObject();
            formsVo.setFid(form.getFid());
            formsVo.setOid(form.getOid());
            //查询员工姓名
            Emp emp = empDao.findById(order.getEid());
            formsVo.setEname(emp.getEname());
            //查询订单状态
            OrderState state = stateDao.findById(order.getSid());
            formsVo.setOrderState(state.getState());
            formsVo.setBuyTime(order.getBuytime());
            //查询商品信息
            Commodity commodity = commodityDao.findById(order.getGid());
            formsVo.setGname(commodity.getGname());
            formsVo.setCount(order.getCount());
            //计算订单总价
            formsVo.setMoney(commodity.getPrice()*order.getCount());
            //查询商品供应商
            Supplier supplier = supplierdDao.findBySupid(commodity.getSupid());
            formsVo.setSupname(supplier.getSupname());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return formsVo;
    }

    @Override
    public List<ReportFormsViewObject> findAll() {
        List<ReportFormsViewObject> formsViews = new ArrayList<>();
        List<ReportForms> forms = new ArrayList<>();
        try {
            //查询全部报表信息
            forms = formDao.findAll();
            //遍历集合 设置报表视图显示对象
            for (ReportForms form:forms){
                //根据报表对象的订单编号查询出对应的订单信息
                Order order = orderDao.findById(form.getOid());
                //实例化报表显示对象并设置值
                ReportFormsViewObject formsVo = new ReportFormsViewObject();
                formsVo.setFid(form.getFid());
                formsVo.setOid(form.getOid());
                //查询员工姓名
                Emp emp = empDao.findById(order.getEid());
                formsVo.setEname(emp.getEname());
                //查询订单状态
                OrderState state = stateDao.findById(order.getSid());
                formsVo.setOrderState(state.getState());
                formsVo.setBuyTime(order.getBuytime());
                //查询商品信息
                Commodity commodity = commodityDao.findById(order.getGid());
                formsVo.setGname(commodity.getGname());
                formsVo.setCount(order.getCount());
                //计算订单总价
                formsVo.setMoney(commodity.getPrice()*order.getCount());
                //查询商品供应商
                Supplier supplier = supplierdDao.findBySupid(commodity.getSupid());
                formsVo.setSupname(supplier.getSupname());
                formsViews.add(formsVo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return formsViews;
    }

    @Override
    public List<ReportFormsViewObject> findAllSpilt(Integer currentPage, Integer pageSize) {
        List<ReportFormsViewObject> formsViews = new ArrayList<>();
        List<ReportForms> forms = new ArrayList<>();
        try {
            //查询全部报表信息
            forms = formDao.findAllSpilt(currentPage,pageSize);
            //遍历集合 设置报表视图显示对象
            for (ReportForms form:forms){
                //根据报表对象的订单编号查询出对应的订单信息
                Order order = orderDao.findById(form.getOid());
                //实例化报表显示对象并设置值
                ReportFormsViewObject formsVo = new ReportFormsViewObject();
                formsVo.setFid(form.getFid());
                formsVo.setOid(form.getOid());
                //查询员工姓名
                Emp emp = empDao.findById(order.getEid());
                formsVo.setEname(emp.getEname());

                //查询订单状态
                OrderState state = stateDao.findById(order.getSid());
                formsVo.setOrderState(state.getState());
                formsVo.setBuyTime(order.getBuytime());

                //查询商品信息
                Commodity commodity = commodityDao.findById(order.getGid());
                formsVo.setGname(commodity.getGname());
                formsVo.setCount(order.getCount());

                //计算订单总价
                formsVo.setMoney(commodity.getPrice()*order.getCount());

                //查询商品供应商
                Supplier supplier = supplierdDao.findBySupid(commodity.getSupid());
                formsVo.setSupname(supplier.getSupname());
                formsViews.add(formsVo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return formsViews;
    }

    @Override
    public List<ReportFormsViewObject> findAllSpilt(Integer currentPage, Integer pageSize, String column, String keyWord) {
        List<ReportFormsViewObject> formsViews = new ArrayList<>();
        List<ReportForms> forms = new ArrayList<>();
        try {
            //查询全部报表信息
            forms = formDao.findAllSpilt(currentPage,pageSize,column,keyWord);
            //遍历集合 设置报表视图显示对象
            for (ReportForms form:forms){
                //根据报表对象的订单编号查询出对应的订单信息
                Order order = orderDao.findById(form.getOid());
                //实例化报表显示对象并设置值
                ReportFormsViewObject formsVo = new ReportFormsViewObject();
                formsVo.setFid(form.getFid());
                formsVo.setOid(form.getOid());
                //查询员工姓名
                Emp emp = empDao.findById(order.getEid());
                formsVo.setEname(emp.getEname());
                //查询订单状态
                OrderState state = stateDao.findById(order.getSid());
                formsVo.setOrderState(state.getState());
                formsVo.setBuyTime(order.getBuytime());
                //查询商品信息
                Commodity commodity = commodityDao.findById(order.getGid());
                formsVo.setGname(commodity.getGname());
                formsVo.setCount(order.getCount());
                //计算订单总价
                formsVo.setMoney(commodity.getPrice()*order.getCount());
                //查询商品供应商
                Supplier supplier = supplierdDao.findBySupid(commodity.getSupid());
                formsVo.setSupname(supplier.getSupname());
                formsViews.add(formsVo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return formsViews;
    }

    @Override
    public Long getAllCount() {
        long num = 0L;
        try {
            //查询全部数据总量
            num = formDao.getAllCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public Long getAllCount(String colunm, String keyWord) {
        long num = 0L;
        try {
            //模糊查询全部数据总量
            num = formDao.getAllCount(colunm,keyWord);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }
}
